package xin.yangshuai.juc01.hello;

import java.util.Random;
import java.util.concurrent.CyclicBarrier;
import java.util.concurrent.TimeUnit;
import java.util.concurrent.locks.Lock;
import java.util.concurrent.locks.ReentrantLock;

/**
 * Question6
 *
 * @author shuai
 * @date 2022/2/13
 */
public class Question6 {

}

class Result6 {


    public static void main(String[] args) {
        CyclicBarrier cyclicBarrier = new CyclicBarrier(6, new Runnable() {

            int count = 0;

            @Override
            public void run() {
                System.out.println();
                System.out.print(Thread.currentThread().getName() + " ::==================" + (count++) + "==================");
                try {
                    TimeUnit.MILLISECONDS.sleep(100);
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }
            }
        });

        Lock lock = new ReentrantLock();

        final Integer[] index = {0};

        for (int i = 1; i <= 6; i++) {
            new Thread(new Runnable() {
                @Override
                public void run() {

                    int step = 0;
                    boolean finishBoolean = false;
                    int current = 0;
                    int cycle = 0;

                    for (int j = 0; j < 50; j++) {

                        try {
                            lock.lock();

                            System.out.println();

                            for (int k = 0; k < step; k++) {
                                System.out.print("*");
                            }

                            if (finishBoolean) {

                                System.out.print(Thread.currentThread().getName());

                                System.out.print(" :: ");
                                System.out.print(cycle);
                                System.out.print(" :: ");

                                if (current == 1) {
                                    System.out.print("winner");
                                } else {
                                    System.out.print(current);
                                }
                            } else {
                                int nextInt = new Random().nextInt(3);
                                step += nextInt;
                                for (int k = 0; k < nextInt; k++) {
                                    System.out.print("*");
                                }

                                System.out.print(Thread.currentThread().getName());

                                if (step >= 50) {
                                    System.out.print(" :: ");
                                    finishBoolean = true;
                                    index[0] = index[0] + 1;
                                    current = index[0];
                                    cycle = j;
                                    System.out.print(cycle);
                                    System.out.print(" :: ");
                                    if (current == 1) {
                                        System.out.print("winner");
                                    } else {
                                        System.out.print(current);
                                    }
                                }
                            }
                        } finally {
                            lock.unlock();
                        }
                        try {
                            cyclicBarrier.await();
                        } catch (Exception e) {
                            e.printStackTrace();
                        }
                    }
                }
            }, String.valueOf(i)).start();
        }
    }
}
